package com.aiguigu.block;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(3);

       /* System.out.println(blockingQueue.add(1));
        System.out.println(blockingQueue.add(2));
        System.out.println(blockingQueue.add(3));
        //System.out.println(blockingQueue.add(4)); //队列满了  java.lang.IllegalStateException
        System.out.println(blockingQueue.element());

        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
       // System.out.println(blockingQueue.remove());//队列为空为抛java.util.NoSuchElementException*/


       /* System.out.println(blockingQueue.offer(1));
        System.out.println(blockingQueue.offer(2));
        System.out.println(blockingQueue.offer(3));
        System.out.println(blockingQueue.offer(3));
        System.out.println("================");
        System.out.println(blockingQueue.peek());
        System.out.println("================");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());*/



       /* blockingQueue.put(1);
        blockingQueue.put(2);
        blockingQueue.put(3);
        System.out.println("----------" + "\t"+blockingQueue.size());
//        blockingQueue.put(4);
        System.out.println("=============" + "\t"+blockingQueue.size());

        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());*/

        System.out.println(blockingQueue.offer(1, 2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer(2, 2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer(3, 2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer(4, 2, TimeUnit.SECONDS));

        System.out.println(blockingQueue.poll( 2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll( 2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll( 2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll( 2, TimeUnit.SECONDS));


    }
}
